package com.huopaolan.yohuoshop.Mvp.View.Fragment;

import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;

import com.huopaolan.lib_core.Base.BaseFragment;
import com.huopaolan.yohuoshop.Adapter.ShoesShopAdapter;
import com.huopaolan.yohuoshop.Bean.ShoesEntity;
import com.huopaolan.yohuoshop.Di.Compont.DaggerShoesFragmentCompont;
import com.huopaolan.yohuoshop.Di.Modules.ShoesFragmentModules;
import com.huopaolan.yohuoshop.Listener.HomeBannerImageLoader;
import com.huopaolan.yohuoshop.Mvp.Contract.ShoesFragmentContract;
import com.huopaolan.yohuoshop.Mvp.Presenter.ShoesFragmentPresenter;
import com.huopaolan.yohuoshop.Mvp.View.Activity.HomeActivity;
import com.huopaolan.yohuoshop.NetWork.API;
import com.huopaolan.yohuoshop.R;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ShoesFragment extends BaseFragment<ShoesFragmentPresenter> implements ShoesFragmentContract.ShoesIView, BaseRefreshListener {
    private PullToRefreshLayout pullToRefreshLayout;
    private RecyclerView shoes_recycler;
    private View shoes_head;
    private Banner shoes_banner;
    private ShoesShopAdapter adapter;
    List<ShoesEntity.Banner> bannerList = new ArrayList<>();
    List<ShoesEntity.Brand> brandList = new ArrayList<>();
    List<ShoesEntity.Values> valuesList = new ArrayList<>();

    @Override
    public View createView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_shoes, null);
    }

    @Override
    public void onStart() {
        Window window = getActivity().getWindow();
        window.setStatusBarColor(Color.BLACK);
        super.onStart();
    }

    @Override
    public void initView(View view) {
        //初始化控件
        pullToRefreshLayout = view.findViewById(R.id.shoes_pulltorefresh_layout);
        pullToRefreshLayout.setRefreshListener(this);
        shoes_recycler = view.findViewById(R.id.shoes_rv);
        //添加头布局
        shoes_head = View.inflate(getActivity(), R.layout.item_shoes_banner, null);
        shoes_banner = shoes_head.findViewById(R.id.shoes_banner);
        //配置数据
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false);
        shoes_recycler.setLayoutManager(manager);
        adapter = new ShoesShopAdapter(R.layout.item_shoes_brand);
        adapter.addHeaderView(shoes_head, 0);
        shoes_recycler.setAdapter(adapter);
    }

    @Override
    public void initData() {
        p.requestShoes();
    }

    @Override
    public void setUpDagger() {
        DaggerShoesFragmentCompont.builder()
                .shoesFragmentModules(new ShoesFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void shoesSuccess(Map<String, List> entity) {
        bannerList.addAll(entity.get("bannerList"));
        List<String> banner = new ArrayList<>();
        for (ShoesEntity.Banner image : bannerList) {
            banner.add(API.BaseUrl + image.recommend_url);
        }
        shoes_banner.setImages(banner);
        shoes_banner.isAutoPlay(true);
        shoes_banner.setIndicatorGravity(BannerConfig.CENTER);
        shoes_banner.setDelayTime(1 * 1500);
        shoes_banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        shoes_banner.setImageLoader(new HomeBannerImageLoader());
        shoes_banner.start();
        brandList.addAll(entity.get("brandList"));
        valuesList.addAll(entity.get("valuesList"));
        adapter.addData(valuesList);
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void refresh() {
        adapter.getData().clear();
        p.requestShoes();
        pullToRefreshLayout.finishRefresh();
    }

    @Override
    public void loadMore() {
        adapter.getData().clear();
        p.requestShoes();
        pullToRefreshLayout.finishLoadMore();
    }
}
